package avicit.bdp.dds.api.controller;

import avicit.bdp.common.utils.DateUtils;
import avicit.bdp.dds.TestUtils;
import avicit.bdp.dds.dao.entity.AlgoNodes;
import avicit.platform6.core.rest.msg.ResponseMsg;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.core.io.FileSystemResource;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import java.util.LinkedHashMap;
import java.util.List;

import static org.junit.Assert.assertEquals;

public class AlgoNodeTest {
    private RestTemplate restTemplate = new RestTemplate();

    private static String fileName = "node_" + System.currentTimeMillis() + ".py";
    private static String typeId = "typeId_" + System.currentTimeMillis();
    private static String projectId = "projectId_" + System.currentTimeMillis();

    /**
     * 所有用例执行前准备工作
     */
    @BeforeClass
    public static void init() {
        System.out.println("用例初始化...");
        TestUtils.createFile(fileName);
    }

    /**
     * 单个测试用例运行前准备工作
     */
    @Before
    public void setUp() {
        // TODO:
    }

    /**
     * 单个测试用例运行后销毁工作
     */
    @After
    public void tearDown() {
        // TODO:
    }

    /**
     * 所有用例执行完成后准备工作
     */
    @AfterClass
    public static void destory() {
        System.out.println("用例执行完成，开始清理...");
        TestUtils.deleteFile(fileName);
    }

    /***********************************************Begin 测试用例 ***********************************************/
    @Test(timeout = 5 * 60 * 1000)
    public void testFile() throws Exception {
        // 查询空间列表
        ResponseEntity<ResponseMsg> result = restTemplate.exchange(TestUtils.HOSTPORT + "/api/bdp/dds/project/getAllProjectList",
                HttpMethod.GET, TestUtils.buildGetHttpEntity(), ResponseMsg.class);

        TestUtils.handleFailedRequest(result);
        assertEquals("200", result.getBody().getRetCode());

        List<Object> finalBody = (List<Object>) result.getBody().getResponseBody();
        if(finalBody != null && finalBody.size() > 0){
            LinkedHashMap<String, String> obj = (LinkedHashMap<String, String>) finalBody.get(0);
            projectId = obj.get("id");
        }

        // 查询根分类
        String startTime = DateUtils.getTodayStartTime();
        String endTime = DateUtils.getTodayEndTime();
        result = restTemplate.exchange(TestUtils.DMS_HOSTPORT + "/api/bdp/dms/das/commontype/findCommonType?type=nodes&projectId="+projectId,
                HttpMethod.GET, TestUtils.buildGetHttpEntity(), ResponseMsg.class);

        TestUtils.handleFailedRequest(result);
        assertEquals("200", result.getBody().getRetCode());

        finalBody = (List<Object>) result.getBody().getResponseBody();
        assertEquals(1, finalBody.size());

        LinkedHashMap<String, String> obj = (LinkedHashMap<String, String>) finalBody.get(0);
        String parentId = obj.get("id");
        String parentName = obj.get("name");
        System.out.println("分类 id = " + parentId + ",name = " + parentName);

        // 新增分类
        JSONObject param = new JSONObject();
        param.put("leaf", 1);
        param.put("name", "aaa");
        param.put("parentId", parentId);
        param.put("parentName", parentName);
        param.put("projectId", projectId);
        param.put("type", "nodes");
        param.put("unfolded", 0);

        result = restTemplate.postForEntity(TestUtils.DMS_HOSTPORT + "/api/bdp/dms/das/commontype/insertCommonType",
                TestUtils.buildPostHttpEntity(param.toJSONString()), ResponseMsg.class);

        TestUtils.handleFailedRequest(result);
        assertEquals("200", result.getBody().getRetCode());

        String type_id = (String) result.getBody().getResponseBody();
        System.out.println("新增的分类ID = " + type_id);

        // 修改分类
        param.put("id", type_id);
        param.put("actionKey", "edit");
        param.put("orderBy", 0);
        param.put("name", "aaa_1");
        result = restTemplate.postForEntity(TestUtils.DMS_HOSTPORT + "/api/bdp/dms/das/commontype/updateCommonType",
                TestUtils.buildPostHttpEntity(param.toJSONString()), ResponseMsg.class);

        TestUtils.handleFailedRequest(result);
        assertEquals("200", result.getBody().getRetCode());

        // 删除分类
        JSONObject param0 = new JSONObject();
        param0.put("id", type_id);
        result = restTemplate.postForEntity(TestUtils.DMS_HOSTPORT + "/api/bdp/dms/das/commontype/deleteCommonType",
                TestUtils.buildPostHttpEntity(param0.toJSONString()), ResponseMsg.class);

        TestUtils.handleFailedRequest(result);
        assertEquals("200", result.getBody().getRetCode());

        // 上传文件
        FileSystemResource fileSystemResource = new FileSystemResource(System.getProperty("user.dir") + "/" + fileName);
        MultiValueMap<String, Object> form = new LinkedMultiValueMap<>();

        form.add("name", fileName);
        form.add("programType", 0);
        form.add("typeId", typeId);
        form.add("projectId", projectId);
        form.add("open", 0);
        form.add("className", "a");
        form.add("file", fileSystemResource);

        result = restTemplate.postForEntity(TestUtils.HOSTPORT + "/api/bdp/dds/nodes/insertNode",
                TestUtils.buildUploadFileHttpEntity(form), ResponseMsg.class);

        TestUtils.handleFailedRequest(result);
        assertEquals("200", result.getBody().getRetCode());
        String id = (String) result.getBody().getResponseBody();

        // 查询算法库列表
        result = restTemplate.exchange(TestUtils.HOSTPORT + "/api/bdp/dds/nodes/getList?projectId="+projectId+"&typeId="+typeId,
                HttpMethod.GET, TestUtils.buildGetHttpEntity(), ResponseMsg.class);

        TestUtils.handleFailedRequest(result);
        assertEquals("200", result.getBody().getRetCode());

        LinkedHashMap<String, Object> map = (LinkedHashMap<String, Object>) result.getBody().getResponseBody();
        finalBody = (List<Object>) map.get("result");
        AlgoNodes node = null;
        for (Object object : finalBody) {
            LinkedHashMap<String, String> nodeObj = (LinkedHashMap<String, String>) object;
            node = JSON.parseObject(JSON.toJSONString(nodeObj), AlgoNodes.class);
            if (node.getId().equals(id)) {
                break;
            }
        }

        // 修改算法库
        if(node != null){
            result = restTemplate.postForEntity(TestUtils.HOSTPORT + "/api/bdp/dds/nodes/updateNode",
                    TestUtils.buildPostHttpEntity(JSON.toJSONString(node)), ResponseMsg.class);

            TestUtils.handleFailedRequest(result);
            assertEquals("200", result.getBody().getRetCode());
        }

        // 删除算法库
        JSONObject param1 = new JSONObject();
        param1.put("id", id);
        result = restTemplate.postForEntity(TestUtils.HOSTPORT + "/api/bdp/dds/nodes/deleteNode",
                TestUtils.buildPostHttpEntity(param1.toJSONString()), ResponseMsg.class);

        TestUtils.handleFailedRequest(result);
        assertEquals("200", result.getBody().getRetCode());
    }

}
